package trees;

public class RemoveElements {
    public static ListNode removeElements(ListNode head, int val) {
        // 构造一个虚拟头节点
        ListNode header = new ListNode(val - 1);
        // 让虚拟头节点指向node的头节点
        header.next = head;
        // 创建一个前缀节点指向header
        ListNode pre = header;

        // pre->header->1->2->6->3->4->5->6
        // 6

        while (pre.next != null) {
            if (pre.next.val == val) {
                pre.next = pre.next.next;
            } else {
                pre = pre.next;
            }
        }

        return header.next;
    }

    public static void main(String[] args) {
        int[] nodeVal = new int[]{1, 2, 6, 3, 4, 5, 6};
        ListNode node = new ListNode(1);
        for (int i = 1; i < nodeVal.length; i++) {
            ListNode.add(nodeVal[i], node);
        }

        ListNode result = removeElements(node, 6);
        while (result != null) {
            System.out.print(result.val + " ");
            result = result.next;
        }

    }
}
